package com.zryc.shop.ums.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.ums.entity.Menu;
import com.zryc.shop.ums.service.IMenuService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 后台菜单表 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-02
 */
@RestController
@RequestMapping("/ums/menu")
@Api(tags = "菜单管理接口列表")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    @PostMapping("updateHidden/{id}")
    @ApiOperation("修改菜单隐藏属性")
    public ResponseResult updateHidden(@PathVariable @ApiParam(value = "菜单ID", required = true) Long id,
                                       @RequestParam @ApiParam(value = "隐藏属性：1-隐藏 0显示", required = true, allowableValues = "0,1") Integer hidden) {
        UpdateWrapper<Menu> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("hidden", hidden);
        menuService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }

    @GetMapping("/{id}")
    @ApiOperation("查询菜单详情")
    public ResponseResult detail(@PathVariable @ApiParam(value = "菜单ID", required = true) Long id) {
        return new ResponseResult(200, "查询完成", menuService.getById(id));
    }

    @PostMapping("/update/{id}")
    public ResponseResult update(@PathVariable @ApiParam(value = "菜单ID", required = true) Long id,
                                 @RequestBody Menu menu) {
        menu.setId(id);
        menuService.updateById(menu);
        return new ResponseResult(200, "修改成功");
    }

    @PostMapping("/create")
    @ApiOperation("创建新菜单")
    public ResponseResult create(@RequestBody Menu menu) {
        menu.setCreateTime(LocalDateTime.now());
        if (menu.getParentId() == 0)//判断是否是顶级菜单
            menu.setLevel(0);
        else
            menu.setLevel(1);
        menuService.save(menu);
        return new ResponseResult(200, "创建成功");
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除菜单")
    public ResponseResult delete(@PathVariable @ApiParam(value = "菜单ID", required = true) Long id) {
//        menuService.removeById(id);
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id)
                .or()
                .eq("parent_id", id);//删除菜单及其子菜单
        menuService.remove(queryWrapper);
        return new ResponseResult(200, "删除成功");
    }

    @GetMapping("/list/{parentId}")
    @ApiOperation("查询菜单列表")
    public ResponseResult list(@PathVariable @ApiParam(value = "父级菜单ID", required = true) Long parentId,
                               @RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                               @RequestParam(defaultValue = "10") @ApiParam(value = "每页条数", required = true) Integer pageSize) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        Page page = new Page(pageNum, pageSize);
        menuService.page(page, queryWrapper);
        return new ResponseResult(200, "查询完成", page);
    }

    @GetMapping("/treeList")
    @ApiOperation("查询菜单的树状图")
    public ResponseResult treeList() {
        //首先要查询0级菜单，且未被隐藏的
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level", 0);
        queryWrapper.eq("hidden", 0);
        List<Menu> menuList = menuService.list(queryWrapper);
        for (Menu parent:menuList) {
            queryWrapper.clear();//在设置新的查询条件之前，将原来的查询条件清除掉
            queryWrapper.eq("parent_id", parent.getId());
            queryWrapper.eq("hidden", 0);
            parent.setChildren(menuService.list(queryWrapper));//查询父节点的全部子节点
        }
        return new ResponseResult(200, "查询完成", menuList);
    }
}
